/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Describe：创建菜单的合集
* CreateTime：2020-04-05 19:06:34
* Version：1.0
* Modify Recorder：
*************************************************/

using System.Collections.Generic;
using System.IO;
using NUnit.Framework.Constraints;
using UnityEditor;
using UnityEngine;
using TinyToolKit;
using TinyToolKit.Editor;
using UnityEditor.SceneManagement;

namespace TinyToolKitEditor
{
    public static class CreateMenuItem
    {
        [MenuItem("TinyToolKit/Play First Scene", false, 0)]
        public static void PlayFirstScene()
        {
            EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo();
            if (EditorBuildSettings.scenes.Length > 0)
            {
                string scenePath = EditorBuildSettings.scenes[0].path;
                EditorSceneManager.OpenScene(scenePath);
                EditorApplication.isPlaying = true;
            }
            else
            {
                EditorUtility.DisplayDialog("警告", "请先添加场景到 Building Settings 窗口中！", "确认");
            }
        }

        [MenuItem("TinyToolKit/Window/TinyToolKit %_t")]
        private static void ShowTinyToolKitWindow()
        {
            TinyToolWindow.ShowWindow();
        }

        [MenuItem("TinyToolKit/Window/Info Library")] 
        private static void ShowInfoLibraryWindow()
        {
            TinyInfoLibraryWindow.ShowWindow();
        }

        [MenuItem("TinyToolKit/Create/TinyToolSetting")]
        private static void CreateTinyTool()
        {
            TinyToolSettingAssetEditor.CreateAsset();
        }

        [MenuItem("TinyToolKit/Create/TinyCanvas")]
        private static void CreateTinyCanvas()
        {
            CreatePrefab("TinyCanvas");
        }

        private static void CreatePrefab(string name)
        {
            GameObject prefab = Resources.Load<GameObject>(name);
            if (prefab != null)
            {
                GameObject obj = PrefabUtility.InstantiatePrefab(prefab) as GameObject;
                obj.transform.Reset();
                obj.transform.SetAsLastSibling();
            }
        }

        [MenuItem("TinyToolKit/Create/Project Directory")]
        public static void CreateAppDirectory()
        {
            string rootDirName = "App";
            List<string> dirName = new List<string> {rootDirName};

            TinyToolEditorSettingAsset editorSetting = Resources.Load<TinyToolEditorSettingAsset>("TinyToolEditorSetting");
            if (editorSetting == null)
            {
                dirName.Add(rootDirName + "/Scenes");
            }
            else
            {
                string[] projectDirList = editorSetting.ProjectDirList;
                foreach (var name in projectDirList)
                {
                    dirName.Add(rootDirName + "/" + name);
                }
            }

            for (int i = 0; i < dirName.Count; i++)
            {
                string dirPath = Path.Combine(Application.dataPath, dirName[i]);
                if (Directory.Exists(dirPath) == false)
                    Directory.CreateDirectory(dirPath);
            }

            AssetDatabase.Refresh();
        }

        [MenuItem("TinyToolKit/Open Folder/Data Path")]
        public static void OpenFolderDataPath()
        {
            Application.OpenURL("file://" + Application.dataPath);
        }

        [MenuItem("TinyToolKit/Open Folder/Persistent Data Path")]
        public static void OpenFolderPersistentDataPath()
        {
            Application.OpenURL("file://" + Application.persistentDataPath);
        }

        [MenuItem("TinyToolKit/Open Folder/Streaming Assets Path")]
        public static void OpenFolderStreamingAssetsPath()
        {
            Application.OpenURL("file://" + Application.streamingAssetsPath);
        }

        [MenuItem("TinyToolKit/Open Folder/Temporary Cache Path")]
        public static void OpenFolderTemporaryCachePath()
        {
            Application.OpenURL("file://" + Application.temporaryCachePath);
        }

        [MenuItem("TinyToolKit/Utility/Add Header Comment", true)]
        private static bool AddHeaderCommentValidate()
        {
            return AutoAddScriptComment.AddHeaderCommentValidate();
        }

        [MenuItem("TinyToolKit/Utility/Add Header Comment", false)]
        private static void AddHeaderComment()
        {
            AutoAddScriptComment.AddHeaderComment();
        }

        [MenuItem("TinyToolKit/Utility/Unify Rename", false)]
        [MenuItem("GameObject/Tiny Utility/Unify Rename", false, 11)]
        private static void ShowUnifyRenameWindow()
        {
            UnifyRename.ShowWindow();
        }

        [MenuItem("TinyToolKit/Utility/Switch Font", false)]
        private static void ShowSwitchFontWindow()
        {
            SwitchFontWindow.ShowWindow();
        }

        [MenuItem("TinyToolKit/Export Package %e", false, 3000)]
        private static void Export()
        {
            ExportHelper.Export();
        }
        
//        [MenuItem("TinyToolKit/Utility/Copy Path", true)]
//        [MenuItem("GameObject/Tiny Utility/Copy ObjectPath", true, 12)]
//        private static bool CopyTransformPathValidate()
//        {
//            return Selection.activeTransform != null;
//        }

        [MenuItem("TinyToolKit/Utility/Copy Path")]
        [MenuItem("GameObject/Tiny Utility/Copy Path")]
        [MenuItem("Assets/Tiny Utility/Copy Path")]
        private static void CopyTransformPath()
        {
            Transform selectChild = Selection.activeTransform;

            if (selectChild != null)
            {
                string result = selectChild.name;
                while (selectChild.parent != null)
                {
                    selectChild = selectChild.parent;
                    result = $"{selectChild.name}/{result}";
                }
                Debug.Log(result);
                TinyEditorUtility.Copy(result);
            }
            else
            {
                string[] guids = Selection.assetGUIDs;
                if (guids != null && guids.Length > 0)
                {
                    string assetPath = AssetDatabase.GUIDToAssetPath(guids[0]);
                    Debug.Log(assetPath);
                    TinyEditorUtility.Copy(assetPath);
                }
            }
        }
    }
}